import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {path: '/register', name: 'register', component: () => import('../views/Register.vue')},
    {path: '/login', name: 'login', component: () => import('../views/Login.vue')},
    {path: '/manager', name: 'manager', component: () => import('../views/ManagerLayout.vue'),
        children:[
            {path: 'home', component: () => import('../views/manager/HomeView.vue')},
            {path: 'admin', component: () => import('../views/manager/AdminView.vue')},
            {path: 'user', component: () => import('../views/manager/UserView.vue')},
            {path: 'grade', component: () => import('../views/manager/GradeView.vue')},
            {path: 'news', component: () => import('../views/manager/NewsView.vue')},
            {path: 'activity', component: () => import('../views/manager/ActivityView.vue')},
            {path: 'record', component: () => import('../views/manager/RecordView')},
            {path: 'takeOff', component: () => import('../views/manager/TakeOffView')},
            {path: 'comment', component: () => import('../views/manager/CommentView.vue')},
            {path: 'message', component: () => import('../views/manager/MessageView.vue')},
            {path: 'pAdmin', component: () => import('../views/manager/pAdmin.vue')},
            {path: 'pUser', component: () => import('../views/manager/pUser.vue')},
            {path: 'password', component: () => import('../views/manager/PasswordView.vue')},

        ]
    },
    //   前台路由
    {path: '/front', name: 'front', component: () => import('../views/FrontLayout.vue'),
        // 二级路由
        children:[
            {path: 'index', component: () => import('../views/front/indexView.vue')},
            {path: 'activity', component: () => import('../views/front/ActivityView.vue')},
            {path: 'news', component: () => import('../views/front/NewsView.vue')},
            {path: 'record', component: () => import('../views/front/RecordView.vue')},
            {path: 'newsDetail', component: () => import('../views/front/NewsDetailView.vue')},
            {path: 'message', component: () => import('../views/front/MessageView.vue')},
        ]
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 路由守卫
router.beforeEach((to ,from, next) => {
    if (to.path === '/') {
        return next("/login");
    }
    let user = localStorage.getItem("user");
    if (!user && to.path !== '/login' && to.path !== '/register' && to.path !== '/front/index') {
        return next("/login");
    }
    next();
})


export default router